package com.ctvit.sjk.knowledge.app.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctvit.sjk.knowledge.app.mapper.TabAppAccessTokenMapper;
import com.ctvit.sjk.knowledge.app.pojo.domain.TabAppAccessToken;
import com.ctvit.sjk.knowledge.app.pojo.dto.AppAuthenticationDTO;
import com.ctvit.sjk.knowledge.app.pojo.vo.AppAuthenticationVO;
import com.ctvit.sjk.knowledge.app.service.ITabAppAccessTokenService;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 应用访问令牌Service业务层处理
 *
 * @author heyingcheng
 * @date 2025/3/22
 */
@Slf4j
@Service
public class TabAppAccessTokenServiceImpl extends ServiceImpl<TabAppAccessTokenMapper, TabAppAccessToken> implements ITabAppAccessTokenService {

    @Override
    public JSONObject authentication(AppAuthenticationDTO dto) {
        LambdaQueryWrapper<TabAppAccessToken> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TabAppAccessToken::getAccessToken, dto.getAccessToken());
        TabAppAccessToken entity = getOne(wrapper);

        JSONObject claimsJsonObject = new JSONObject();
        claimsJsonObject.put(AppAuthenticationVO.KEY_APPLICATION_ID, entity.getAppId());
        claimsJsonObject.put(AppAuthenticationVO.KEY_ACCESS_TOKEN, entity.getAccessToken());
        claimsJsonObject.put(AppAuthenticationVO.KEY_USER_ID, entity.getCreateId());
        claimsJsonObject.put(AppAuthenticationVO.KEY_CLIENT_ID, IdWorker.getIdStr());
        claimsJsonObject.put(AppAuthenticationVO.KEY_AUTHENTICATION, new JSONObject());

        return claimsJsonObject;
    }

}
